﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace edit{
	public class Main : MonoBehaviour {
		public static bool hasSaved = false;
		public static string dataKey = "edit_map";
		// -1表示无限
		public int[] counts = {
			-1,	// 空
			-1,	// 砖墙
			-1,	// 草丛
			-1,	// 雪地
			6,	// 河流
			5,	// 铁块
			2,	// 部队投放点
			1	// 维修点
		};
		public static int[] index2code = {
			0,
			21,
			4,
			18,
			17,
			16,
			20,
			3
		};
		public static Dictionary<int, int> code2index;
		public int[,] map = new int[6,13]{
			{0,0,0,0,19,7,1,5,19,0,0,0,0},
			{0,0,0,0,0,8,10,6,0,0,0,0,0},
			{0,0,0,0,0,0,0,0,0,0,0,0,0},
			{0,0,0,0,0,0,0,0,0,0,0,0,0},
			{0,0,0,0,0,0,0,0,0,0,0,0,0},
			{0,0,0,0,0,0,0,0,0,0,0,0,0}
		};
		int[] defaultMapItems = {
			19,7,1,5,
			8,10,6
		};

		Config cfg;
		
		// Use this for initialization
		void Start () {
//PlayerPrefs.DeleteKey (dataKey);

			code2index = new Dictionary<int, int> ();
			code2index.Add (0, 0);
			code2index.Add (21, 1);
			code2index.Add (4, 2);
			code2index.Add (18, 3);
			code2index.Add (17, 4);
			code2index.Add (16, 5);
			code2index.Add (20, 6);
			code2index.Add (3, 7);

			cfg = new Config ();

			// 取数据
			string strData = PlayerPrefs.GetString (dataKey);
			// 字符串转数组
			string[] tmpArr1 = strData.Split (new char[]{';'}, System.StringSplitOptions.RemoveEmptyEntries);
			string[] tmpArr2;
			for (int i = 0; i < tmpArr1.Length; i++) {
				tmpArr2 = tmpArr1[i].Split(new char[]{','}, System.StringSplitOptions.RemoveEmptyEntries);
				for (int j = 0; j < tmpArr2.Length; j++) {
					map[i, j] = int.Parse(tmpArr2[j]);
				}
			}


			// 绘制地图块
			for (int i = 0; i < 6; i++) {
				for (int j = 0; j < 13; j++) {
					if (map[i, j] == 0) {
						Instantiate (Resources.Load("prefab/edit_block"),
						             new Vector3 ((j + 1) * 2 - 1, (i + 1) * 2 - 1, 0),
						             Quaternion.identity
						             );
					}
					else {
						bool isDefaultItem = false;
						for (int k = 0; k < defaultMapItems.Length; k++) {
							if (map[i, j] == defaultMapItems[k]) {
								isDefaultItem = true;
								break;
							}
						}
						if (isDefaultItem) {
							Instantiate (Resources.Load("prefab/" + cfg.codeToName(map[i, j])),
							             new Vector3 ((j + 1) * 2 - 1, (i + 1) * 2 - 1, 0),
							             Quaternion.identity
							             );
						}
						else {
							GameObject obj = Instantiate (Resources.Load("prefab/edit_block"),
							             new Vector3 ((j + 1) * 2 - 1, (i + 1) * 2 - 1, 0),
							             Quaternion.identity
							             ) as GameObject;
							obj.GetComponent<Block>().currIndex = code2index[map[i, j]];
						}
					}


				}
			}
		}
		
		// Update is called once per frame
		void Update () {
			// back键 提示保存
			if ((Application.platform == RuntimePlatform.Android && Input.GetKeyDown(KeyCode.Escape)) || Input.GetKeyDown(KeyCode.Escape)) {
				Block.hideCountLabel = true;
				GameObject dialog = GameObject.FindGameObjectWithTag("DIALOG_EXIT");
				dialog.transform.localScale = new Vector3(1, 1, 1);
			}
		}
	}
}